package com.qf.hospital.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.entity.SysMenu;
import com.qf.hospital.entity.SysMenuExample;
import com.qf.hospital.mapper.SysMenuMapper;
import com.qf.hospital.service.SysMenuService;
import com.qf.hospital.util.ExampleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zjw
 * @description
 * @date 2021/10/19 9:50
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> findAll() {
        //1. 封装条件
        SysMenuExample example = new SysMenuExample();
        SysMenuExample.Criteria criteria = example.createCriteria();
        criteria.andAvailableEqualTo(1);
        //2. 查询并返回
        return sysMenuMapper.selectByExample(example);
    }

    @Override
    public PageInfo<SysMenu> findByTitle(String title,Integer page,Integer limit) {
        //1. 执行分页
        PageHelper.startPage(page,limit);

        //2. 封装查询条件
        SysMenuExample example = new SysMenuExample();
        SysMenuExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(title)){
            criteria.andTitleLike(ExampleUtil.any(title));
        }

        //3. 执行查询
        List<SysMenu> list = sysMenuMapper.selectByExample(example);

        //4. 封装数据
        PageInfo<SysMenu> pageInfo = new PageInfo<>(list);

        //5. 返回
        return pageInfo;
    }

}
